/***************************************
 * 说明：本文件主要列举文中所实现的例题
 * *************************************/
/***************************************
 * 例子1.1：实现一个最简单的C++程序：
 * 输出:Hello World This is C++ Program
 * *************************************/
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
void example1()
{
    // 在此cout为输出流对象（比如显示器）
    // 所以如下语句可以理解为向显示器输出字符Hello World This is C++ Program
    cout << "Hello World This is C++ Program" << endl;
}

/***************************************
 * 例子1.2：求a/b两数之和
 * *************************************/
void example2()
{
    int a, b, sum;
    // 在此cin为输入流对象（比如键盘）
    // 所有如下语句表示将键盘的内容流入a/b变量中
    cin >> a >> b;
    sum = a + b;
    cout << "a + b = " << sum << endl;
}

/***************************************
 * 例子1.3：求a/b两数中的最大值
 * *************************************/
int example3()
{
    int a, b;
    int max;
    cin >> a >> b;
    if (a >= b) {
        max = a;
    } else {
        max = b;
    }
    return max;
}

/****************************************************
 * 例子1.4：包含类的C++程序
 * 定义一个Student类，该类包含两个成员变量以及两个成员函数
 * ***************************************************/
class Student {
public:
    void setData()
    {
        cin >> num;
        cin >> score;
    }
    void display()
    {
        cout << num << endl;
        cout << score << endl;
    }
private:
    int num;
    int score;
};

/****************************************************
 * 例子1.5：用cin/cout实现数据的输入输出
 * ***************************************************/
void example5()
{
    cout << "please enter your name and age:" << endl;
    int age;
    char name[10];
    cin >> age;
    cin >> name;
    cout << "your name is:" << name << endl;
    cout << "your age is:" << age << endl;
}

/****************************************************
 * 例子1.6：求3个数中的最大值（分别考虑整数/实数/长整数）
 * ***************************************************/
int max(int a, int b, int c)
{
    if (a <= b) {
        a = b;
    }
    if (a <= c) {
        a = c;
    }
    return a;
}
float max(float a, float b, float c)
{
    if (a <= b) {
        a = b;
    }
    if (a <= c) {
        a = c;
    }
    return a;
}
double max(double a, double b, double c)
{
    if (a <= b) {
        a = b;
    }
    if (a <= c) {
        a = c;
    }
    return a;
}
void example6()
{
    int a,b,c;
    cin >> a >> b >> c;
    int max_i = max(a,b,c);
    cout << "max_i = " << max_i << endl;

    float d,e,f;
    cin >> d >> e >> f;
    float max_f = max(d, e, f);
    cout << "max_f = " << max_f << endl;

    double g,h,i;
    cin >> g >> h >> i;
    float max_d = max(g, h, i);
    cout << "max_d = " << max_d << endl;
}

/****************************************************
 * 例子1.7：用一个函数求2个整数或者3个整数的最大值
 * ***************************************************/
int max_2(int a, int b)
{
    if (a >= b) {
        return a;
    } else {
        return b;
    }
}
int max_3(int a, int b, int c)
{
    if (a <= b) {
        a = b;
    }
    if (a <= c) {
        a = c;
    }
    return a;
}
void example7()
{
    int a = 7, b = -4, c = 9;
    cout << "max_2 = " << max_2(a,b) << endl;
    cout << "max_3 = " << max_3(a,b,c) << endl;
}

/****************************************************
 * 例子1.8：将上述例子1.6使用函数模板实现
 * ***************************************************/
template<typename type>
type max(type a, type b, type c)
{
    if (a <= b) {
        a = b;
    }
    if (a <= c) {
        a = c;
    }
    return a;
}
void example8()
{
    int a,b,c;
    cin >> a >> b >> c;
    int max_i = max(a, b, c); // 调用函数模板，此时T被int替代
    cout << "max_i = " << max_i << endl;

    float d,e,f;
    cin >> d >> e >> f;
    float max_f = max(d, e, f); // 调用函数模板，此时T被float替代
    cout << "max_f = " << max_f << endl;

    double g,h,i;
    cin >> g >> h >> i;
    float max_d = max(g, h, i); // 调用函数模板，此时T被double替代
    cout << "max_d = " << max_d << endl;
}

/****************************************************
 * 例子1.9：了解引用与变量的关系
 * ***************************************************/
void example9()
{
    int a = 10;
    int &b = a;
    a = a*a;
    cout << a << b << endl;
    b = b/2;
    cout << b << a << endl;
}

/****************************************************
 * 例子1.10：无法实现两个数互换的程序
 * ***************************************************/
void example10(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}
/****************************************************
 * 例子1.11：使用引用的方式交换a/b
 * ***************************************************/
void example11(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}
/****************************************************
 * 例子1.12：使用指针的方式交换a/b
 * ***************************************************/
void example12(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

/****************************************************
 * 例子1.13：将函数指定为内联函数
 * ***************************************************/
inline int max11(int a, int b, int c)
{
    if (a <= b) {
        a = b;
    }
    if (a <= c) {
        a = c;
    }
    return a;
}
void example13()
{
    max11(1, 2, 3);
}

/****************************************************
 * 例子1.14：局部变量与全局变量
 * ***************************************************/
float temp = 12.34;
void example14()
{
    int temp = 5;
    cout << temp << endl; // 调用局部变量temp
    cout << ::temp << endl; // 调用全局变量temp
}

/****************************************************
 * 例子1.15：输入3个字符串，要求按字母有小到大输出
 * ***************************************************/
void example15()
{
    string str1, str2, str3;
    cin >> str1 >> str2 >> str3;
    cout << "please input three string" << endl;
    // str2 <= str3
    if (str2 > str3) {
        string temp = str2;
        str2 = str3;
        str3 = temp;
    }
    if (str1 <= str2) {
        cout << str1 << " " << str2 << " " << str3 << endl;
    }
    else if (str1 <= str3) {
        cout << str2 << " " << str1 << " " << str3 << endl;
    } else {
        cout << str2 << " " << str3 << " " << str1 << endl;
    }
}

/****************************************************
 * 例子1.16：开辟空间用以存放一个结构体变量
 * ***************************************************/
struct Student2 {
    char name[10];
    int age;
    char sex;
};
void example16()
{
    Student2* stu = new Student2;
    strcpy(stu->name, "dongfang");
    stu->age = 20;
    stu->sex = 'M';
    cout << stu->name << " " << stu->age << " " << stu->sex << endl;
    delete stu;
}

int main()
{
    return 0;
}